﻿using EntytiBookShop.DAL;
using EntytiBookShop.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EntytiBookShop
{
    public partial class AddBookForm : Form
    {
        //string bookTitle;
        Author author;
        //Category category;
        public AddBookForm()
        {

            InitializeComponent();
         //   Uri uri = new Uri(@"\Images\not_photo2.jpg", UriKind.Relative);
            string path = Environment.CurrentDirectory.ToString()+ "\\Images\\not_photo2.jpg";
            AddImageToPictureBox(path);
            ShowCategoriesAndAuthors();
        }
        private void AddImageToPictureBox(string path)
        {
            Bitmap image = new Bitmap(path);
            pictureBox1.Image = image;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void btAddImage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            else
            {
                tbAddImage.Text = openFileDialog1.FileName;
                Bitmap img = new Bitmap(openFileDialog1.FileName);
                pictureBox1.Image = img;
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                
            }

        }
        private void ShowCategoriesAndAuthors()
        {
            lbCategories.Items.Clear();
            using (BookShopContext ctx = new BookShopContext())
            {
                
                List<Category> Categories = ctx.Categories.ToList();
                foreach (Category cat in Categories)
                {

                    lbCategories.Items.Add(new ListItem(cat.CategoryID, cat.CategoryName));
                }
            }
            //using (BookShopContext ctx = new BookShopContext())
            //{
            //    var authors = ctx.Authors.ToList();
            //    int i = 0; 
            //    foreach (var auth in authors)
            //    {
            //        dgvAuthors.Rows[i].Cells[0].Value = auth.AuthorID;
            //        dgvAuthors.Rows[i].Cells[1].Value = auth.LastName;
            //        dgvAuthors.Rows[i].Cells[2].Value = auth.FirstName;
            //        i++;

            //    }
            //}
            using (BookShopContext ctx = new BookShopContext())
            {
                Dictionary<int, string> authorsList = new Dictionary<int,string>();
                var authors = ctx.Authors.ToList();
                foreach (var auth in authors)
                {
                    authorsList.Add(auth.AuthorID, String.Format(auth.LastName + " " + auth.FirstName));
                }
                
                lbAuthors.DataSource = new BindingSource(authorsList, null);
                lbAuthors.ValueMember = "Key";
                lbAuthors.DisplayMember = "Value";



            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btAddBook_Click(object sender, EventArgs e)
        {

            Book book = new Book();
        }

        private void lbAuthors_SelectedIndexChanged(object sender, EventArgs e)
        {
            string idString = lbAuthors.SelectedValue.ToString();
            using (BookShopContext ctx = new BookShopContext())
            {
                List<Author> authors = ctx.Authors.ToList();
                
                //author = authors.Where(a => a.AuthorID == Convert.lbAuthors.SelectedValue.ToString());
            }

      //      string aName = author.LastName;
            //using (BookShopContext ctx = new BookShopContext())
            //{
            //    var authors = ctx.Authors.ToList();
            //    foreach (var auth in authors)
            //    {
            //        int a = (int)lbAuthors.SelectedValue;
            //    }
            //}
        }
    }
}
